import { Component, EventEmitter, OnInit, Input, Output } from '@angular/core';
import { QuestionModel } from '../../../model/question.model';

@Component({
  moduleId: module.id,
  selector: 'app-question-radio',
  templateUrl: './question-radio.component.html',
  styleUrls: ['./question-radio.component.scss']
})
export class QuestionRadioComponent implements OnInit {
  @Input()
  question: QuestionModel;
  @Input()
  editable: boolean;
  @Output()
  deleteQuestionRequest: EventEmitter<any> = new EventEmitter();

  private key: number;

  constructor() { }

  ngOnInit() {
    this.copyQuestion();
    let options = this.question.options;
    this.key = options[options.length - 1].key;
  }
// 用于删除问题选项， 如果选项个数小于等于1时， 将不允许删除，以保证至少存在一个选项
  onDeleteOption(index: number) {
    if (this.question.options.length <= 1) {
      return;
    }
    this.question.options.splice(index, 1);
  }
// 用于添加新的问题选项， 每增加一个选项key值将加1
  onAddOption() {
    this.question.options.push({push: ++this.key, value: ''});
  }

}
